{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-24T07:35:16.800352Z",
     "start_time": "2025-04-24T07:35:16.786352Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "\n",
    "# 定义信息熵计算函数\n",
    "def entropy(y):\n",
    "    unique_labels, counts = np.unique(y, return_counts=True)\n",
    "    probabilities = counts / counts.sum()\n",
    "    entropy = -np.sum(probabilities * np.log2(probabilities))\n",
    "    return entropy\n",
    "\n",
    "# 定义信息增益计算函数\n",
    "def information_gain(X, y, feature_index):\n",
    "    total_entropy = entropy(y)\n",
    "    values, counts = np.unique(X[:, feature_index], return_counts=True)\n",
    "    weighted_entropy = 0\n",
    "    for value, count in zip(values, counts):\n",
    "        subset = y[X[:, feature_index] == value]\n",
    "        weighted_entropy += (count / np.sum(counts)) * entropy(subset)\n",
    "    info_gain = total_entropy - weighted_entropy\n",
    "    return info_gain\n",
    "\n",
    "# 定义增益率计算函数\n",
    "def gain_ratio(X, y, feature_index):\n",
    "    info_gain = information_gain(X, y, feature_index)\n",
    "    values, counts = np.unique(X[:, feature_index], return_counts=True)\n",
    "    intrinsic_value = -np.sum((counts / np.sum(counts)) * np.log2(counts / np.sum(counts)))\n",
    "    gain_ratio = info_gain / intrinsic_value if intrinsic_value != 0 else 0\n",
    "    return gain_ratio\n",
    "\n",
    "# 创建一个简单的数据集\n",
    "X = np.array([[0, 0],\n",
    "              [0, 1],\n",
    "              [1, 0],\n",
    "              [1, 1]])\n",
    "y = np.array([0, 1, 1, 0])\n",
    "\n",
    "# 计算信息熵\n",
    "entropy_value = entropy(y)\n",
    "\n",
    "# 计算第一个特征的信息增益和增益率\n",
    "info_gain_value = information_gain(X, y, 0)\n",
    "gain_ratio_value = gain_ratio(X, y, 0)\n",
    "\n",
    "entropy_value, info_gain_value, gain_ratio_value"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1.0, 0.0, 0.0)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
